package com.gable.exhibition.controller;

import com.alibaba.fastjson.JSON;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.gable.common.util.CacheData;
import com.gable.common.util.FileUploadUtil;
import com.gable.common.util.ReturnResultUtil;
import com.gable.common.util.StringUtil;
import com.gable.exhibition.model.CommentInfo;
import com.gable.exhibition.model.Upkeep;
import com.gable.exhibition.model.User;
import com.gable.exhibition.service.*;
import com.gable.exhibition.util.MessageUtil;
import com.gable.exhibition.view.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Sjx on 2018/3/7.
 */
@Controller
@RequestMapping(value="/comment")
public class CommentController {
    Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    CommentService commentService;

    @Autowired
    RepairService repairService;

    @Autowired
    UpkeepService upkeepService;

    @Autowired
    InstallService installService;

    /**
     * 查询评论
     * @param request
     * @param response
     * @param commentView
     * @return
     */
    @RequestMapping(value="/queryComment",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryComment(HttpServletRequest request, HttpServletResponse response,CommentView commentView){
        try{
            ReturnResultUtil rru = commentService.queryComment(commentView);
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500, MessageUtil.QUERYEXCEPTION);
    }

    /**
     * 查询回复的评论
     * @param request
     * @param response
     * @param commentView
     * @return
     */
    @RequestMapping(value="/queryReplyComment",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil queryReplyComment(HttpServletRequest request, HttpServletResponse response,CommentView commentView){
        try{
            ReturnResultUtil rru = commentService.queryReplyComment(commentView);
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500, MessageUtil.QUERYEXCEPTION);
    }

    /**
     * 新增评论
     * @param request
     * @param response
     * @param commentView
     * @return
     */
    @RequestMapping(value="/addComment",method = RequestMethod.POST, produces="application/json;charset=utf-8")
    @ResponseBody
    public ReturnResultUtil addComment(HttpServletRequest request, HttpServletResponse response,CommentView commentView){
        try {
            if(commentView.getCommentContent() == null) //评论内容不能为空
                return new ReturnResultUtil(500,MessageUtil.COMMENTCONTENTISNULL);

            List<CommentInfoView> cv =JSON.parseArray(commentView.getJsonStr(),CommentInfoView.class);
            commentView.setCommentInfoList(cv);

            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Long userId = null;
            if(!StringUtil.isEmpty(request.getParameter("userId"))){
                userId = Long.parseLong(request.getParameter("userId"));
            }
            User user = CacheData.getSessionUser(userId,1);
            commentView.setUserFrom(user.getUserId());//评论人为当前登录用户
            commentView.setCreatePerson(user.getUserId());
            //被评论方为对应维修、保养、安装的serviceId
            Long keyType = commentView.getKeyType();
            Long keyId = commentView.getKeyId();
            if(keyType==2901){//维修
                RepairView repairView = new RepairView();
                repairView.setRepairId(keyId);
                ReturnResultUtil repair = repairService.details(repairView);
                RepairView rv = (RepairView) repair.getData();
                commentView.setCoverUserFrom(rv.getServiceId());
            }else if(keyType==2902){//保养
                UpkeepView upkeepView = new UpkeepView();
                upkeepView.setUpkeepId(keyId);
                ReturnResultUtil upkeep = upkeepService.queryUpkeepInfo(upkeepView);
                UpkeepView uv = (UpkeepView) upkeep.getData();
                commentView.setCoverUserFrom(uv.getServiceId());
            }else if(keyType==2903){//安装
                ParameterView pv = new ParameterView();
                pv.setInstallId(keyId);
                ReturnResultUtil install = installService.installInfo(pv);
                InstallView iv = (InstallView) install.getData();
                commentView.setCoverUserFrom(iv.getServiceId());
            }else{
                return new ReturnResultUtil(500,MessageUtil.COMMENTINFOISNULL);
            }

            commentView.setCreatePerson(user.getUserId());
            commentView.setCreateTime(df.format(new Date()));
            return commentService.addComment(commentView);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500,MessageUtil.SAVEEXCEPTION);
    }
}
